package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinActivityService;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinApplyService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/11
 * @Description：小程序活动接口
 */
@Controller
@RequestMapping("/sunday/small/activity")
@Api(value = "activity",description = "活动")
public class SundaySmallActivityController extends BaseController{
    @Autowired
    private SundayWeiXinActivityService activityService;
    @Autowired
    private SundayWeiXinApplyService applyService;

    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ApiOperation("【小程序】获取活动列表/我的活动,除了用户id必传其他两个参数择一填写")
    public void select(HttpServletRequest request,HttpServletResponse response
    ,@ApiParam(value = "用户id",required = true)@RequestParam("memberId")Long memberId
    ,@ApiParam(value = "查询已报名传1否则不传",required = false)@RequestParam(value = "isApply",required = false)Integer isApply
    ,@ApiParam(value = "查询首页推荐传1否则不传",required = false)@RequestParam(value = "isRecommend",required = false)Integer isRecommend
     ,@ApiParam(value = "起始页",required = true) @RequestParam(value = "pageNumber",required = true) String pageNumber
     , @ApiParam(value = "每页大小",required = true) @RequestParam(value = "pageSize",required = true)String pageSize,
       @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort,
        @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order){
        try {
            ResponseMessage.success( activityService.select(request,memberId,isApply,isRecommend));

        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/activity/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @ApiOperation("【小程序】用户获取活动详情")
    @RequestMapping(value = "/getDetail",method = RequestMethod.POST)
    public void getDetail(HttpServletRequest request,HttpServletResponse response
    ,@ApiParam(value = "活动id",required = true)@RequestParam(value = "activityId")Long activityId
    ,@ApiParam(value = "用户id",required = true)@RequestParam("memberId")Long memberId){
        try {
            ResponseMessage.success( activityService.findOne(memberId,activityId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/activity/getDetail---e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
    @ApiOperation(value = "【小程序】用户报名参加活动")
    @RequestMapping(value = "/apply",method = RequestMethod.POST)
    public void apply(HttpServletRequest request,HttpServletResponse response
            ,@ApiParam(value = "用户id",required = true)@RequestParam("memberId") Long memberId
            ,@ApiParam(value = "用户名字",required = true)@RequestParam("name") String name
            ,@ApiParam(value = "用户手机号",required = true)@RequestParam("mobile") String mobile
            ,@ApiParam(value = "微信",required = true)@RequestParam("weChat") String weChat
            ,@ApiParam(value = "参与人数",required = true)@RequestParam("num") Integer num
            ,@ApiParam(value = "活动id",required = true)@RequestParam("activityId")Long activityId
            ,@ApiParam(value = "验证码",required = true)@RequestParam("code")String code){
        try {
            applyService.save( memberId, name,mobile, weChat, num, activityId,code);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer _code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                _code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/activity/apply----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(_code, msg);
        }
    }
}
